(library (bytevector-utils)
  (export
   string->bytevector
   bytevector->string)
  (import
    (except (rnrs base) let-values)
    (only (guile)
          lambda* λ
          error
          eof-object?
          call-with-output-string
          call-with-input-string)
    (prefix (logging) log:)
    (ice-9 binary-ports)
    (rnrs bytevectors)))


(define string->bytevector
  (λ (the-string)
    (call-with-input-string the-string
      (λ (port)
        (get-bytevector-all port)))))


(define bytevector->string
  (λ (bytevec)
    "Translate all bytes in a bytevector to characters and make a string from
them. Since bytevectors contain only bytes, which consist of 8 bits, which makes
for 2 to the power of 8 possible values, the characters can only range from the
zeroth character to the 255th character."
    (list->string
     (map integer->char
          (bytevector->u8-list bytevec)))))
